<?php
abstract class Custom_Model_Abstract
{
    protected $_classMethods;
    protected $_cache;
    protected $_cacheOptions = array();

    public function  __construct($options = null)
    {
        if ($options instanceof Zend_Config) {
            $options = $options->toArray();
        }
        if (is_array($options)) {
            $this->setOptions($options);
        }

        $this->init();
    }

    public function init()
    {
    }

    public function setOptions(array $options)
    {
        if (null === $this->_classMethods) {
            $this->_classMethods = get_class_methods($this);
        }
        foreach ($options as $key => $value) {
            $method = 'set' . ucfirst($key);
            if (is_array($method, $this->_classMethods)) {
                $this->$method($value);
            }
        }
        return $this;
    }

    public function setCache(Custom_Model_Cache_Abstract $cache)
    {
        $this->_cache = $cache;
    }

    public function setCacheOptions(array $options)
    {
        $this->_cacheOptions = $options;
    }

    public function getCacheOptions()
    {
        if (empty($this->_cacheOptions)) {
            $frontendOptions = array(
                    'lifetime' => 1800,
                    'automatic_serialization' => true
            );
            $backendOptions = array(
                    'cache_dir'=> APPLICATION_PATH . "/../var/cache/"
            );
            
            $this->_cacheOptions = array(
                    'frontend'        => 'Class',
                    'backend'         => 'File',
                    'frontendOptions' => $frontendOptions,
                    'backendOptions'  => $backendOptions
            );
        }
        return $this->_cacheOptions;
    }

    public function getCached($tagged = null)
    {
        if (null === $this->_cache) {
            $this->_cache = new Custom_Model_Cache(
                    $this,
                    $this->getCacheOptions(),
                    $tagged
            );
        }
        $this->_cache->setTagged($tagged);
        return $this->_cache;
    }
}
?>